package com.jt.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jt.mapper.UserMapper;
import com.jt.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServerImpl implements UserService{

    private static Map<Integer,String> typeMap = new HashMap<>();

    static {        //类加载时就要执行  只执行一次
        typeMap.put(1,"username");
        typeMap.put(2,"phone");
        typeMap.put(3,"email");
    }

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询数据库,检查是否有数据.
     * @param param
     * @param type
     * @return  true 表示数据已存在   false 数据可以使用
     */
    @Override
    public Boolean checkUser(String param, Integer type) {
        String column = typeMap.get(type);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(column,param);
        Integer count = userMapper.selectCount(queryWrapper);
        return count==0?false:true;
    }

    //测试HtpClient
    @Override
    public void saveHtpClient(User user) {
        user.setEmail("145@qq.com").setPhone("13247521154278").setCreated(new Date()).setUpdated(new Date());
        try {
            userMapper.insert(user);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("失败");
        }
    }
}
